package com.zui.entity.product;

import java.io.File;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.zui.entity.BaseUuidEntity;

@Entity
// product, filename唯一性；
@Table(name = "image")
public class Image extends BaseUuidEntity {
	private static final long serialVersionUID = 1L;
	private static final String MEDIUM_IMAGE_SUFFIX = "_medium";
	private static final String SMALL_IMAGE_SUFFIX = "_small";
	private static final String LIST_VIEW_IMAGE_SUFFIX = "_list";
	private static final String THUMB_VIEW_IMAGE_SUFFIX = "_thumb";

	public static final String DEFAULT_IMAGE_RELATIVE_PATH 
	= "upload" 
	+ System.getProperty("file.separator") 
	+ "images";
	
	public Image() {
	}
	public Image(String id) {
		this.id = id;
	}
	public Image(File uploadedFile, String uploadedFileName, String appContextPath, String relativePath, String realPath) {
		this.uploadedFile = uploadedFile;
		this.uploadedFileName = uploadedFileName;
		this.appContextPath = appContextPath;
		this.relativePath = relativePath;
		this.directory = realPath;
	}
	
	/* --- for haboo use --- */
	private String ownerCode;
	private String ownerType;
	
	/**
	 * The app's context path
	 */
	private String appContextPath;
	/**
	 * real path in the server for the image. for example: C:\webapps\xxx.
	 */
	private String directory;
	/**
	 * relative path of the directory. for example: upload\images\xxx.
	 */
	private String relativePath;
	/**
	 * the original name of the image uploaded.
	 */
	private String uploadedFileName;
	
	private Integer originalImageFileSize;

	@Transient
	public String getOrginalImageFileName() {
		return id + "." + getImageFormat();
	}

	@Transient
	public String getMediumImageFileName() {
		return id + MEDIUM_IMAGE_SUFFIX + "." + getImageFormat();
	}

	@Transient
	public String getSmallImageFileName() {
		return id + SMALL_IMAGE_SUFFIX + "." + getImageFormat();
	}

	@Transient
	public String getListViewImageFileName() {
		return id + LIST_VIEW_IMAGE_SUFFIX + "." + getImageFormat();
	}

	@Transient
	public String getThumbViewImageFileName() {
		return id + THUMB_VIEW_IMAGE_SUFFIX + "." + getImageFormat();
	}

	private File uploadedFile;
	@Transient
	public File getUploadedFile() {	return uploadedFile;}
	
	@Transient
	public String getImageFormat() {
		return uploadedFileName.substring(uploadedFileName.lastIndexOf(".") + 1);
	}	
	
	private String originalImageUrl;
	@Transient
	public String getOriginalImageUrl() {
		return appContextPath + "/" + relativePath.replace("\\", "/") + "/" + getOrginalImageFileName();
	}
	
	private String mediumImageUrl;
	@Transient
	public String getMediumImageUrl() {
		return appContextPath + "/" + relativePath.replace("\\", "/") + "/" + getMediumImageFileName();
	}
	
	private String smallImageUrl;
	@Transient
	public String getSmallImageUrl() {
		return appContextPath + "/" + relativePath.replace("\\", "/") + "/" + getSmallImageFileName();
	}
	
	private String listViewImgUrl;
	@Transient
	public String getListViewImageUrl() {
		return appContextPath + "/" + relativePath.replace("\\", "/") + "/" + getListViewImageFileName();
	}
	private String thumbViewImgUrl;
	@Transient
	public String getThumbViewImageUrl() {
		return appContextPath + "/" + relativePath.replace("\\", "/") + "/" + getThumbViewImageFileName();
	}

	/*------------------------------
	 * getters and annotations
	 --------------------------------*/
	public String getOwnerCode() {
		return ownerCode;
	}
	@Column(length=1)
	public String getOwnerType() {
		return ownerType;
	}
	public String getAppContextPath() {	return appContextPath;	}
	public String getDirectory() {
		return directory;
	}
	public String getRelativePath() {
		return relativePath;
	}
	public String getUploadedFileName() {
		return uploadedFileName;
	}
	public Integer getOriginalImageFileSize() {
		return originalImageFileSize;
	}
	
	
	


	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}
	public void setOwnerCode(String ownerCode) {
		this.ownerCode = ownerCode;
	}
	public void setOwnerType(String ownerType) {
		this.ownerType = ownerType;
	}
	public void setAppContextPath(String appContextPath) {
		this.appContextPath = appContextPath;
	}
	public void setDirectory(String directory) {
		this.directory = directory;
	}
	public void setUploadedFileName(String uploadedFileName) {
		this.uploadedFileName = uploadedFileName;
	}
	public void setUploadedFile(File uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public void setOriginalImageFileSize(Integer originalImageFileSize) {
		this.originalImageFileSize = originalImageFileSize;
	}



	
}
